var qqcs;
var tmp_jsid;

$(function () {
    initJsTree();
});

//初始化角色树
function initJsTree() {
    var str_jslist = $('#jslist_str').val();
    $('#js_tree').tree({
        data: $.evalJSON(str_jslist),
        checkbox: true,
        onlyLeafCheck: true,
        lines: true,
        fit: true,
        onSelect: function (node) {
            var checkedNode = $('#js_tree').tree('getChecked');
            if (checkedNode != null && checkedNode.length > 0) {
                $.each(checkedNode, function (index, value) {
                    $('#js_tree').tree('uncheck', value.target);
                })
            }
            if (node.checked) {
                $('#js_tree').tree('uncheck', node.target);
            }
            else {
                $('#js_tree').tree('check', node.target);
            }
            if ("" != node.id) {
                queryGnListByJsId(node.id);
            }
        },
        onLoadSuccess: function (node, data) {
            //取消checkbox的默认行为
            $(this).find('span.tree-checkbox').unbind().click(function () {
                return false;
            });
        }
    });
    //有角色则按第一个做查询带出功能树
    var nodes = $('#js_tree').tree('getChecked');
    if (null != nodes) {
        queryGnListByJsId(nodes[0].id);
    }
}

//根据角色id查询功能集情况
function queryGnListByJsId(jsid) {
    qqcs = {"jsid": jsid};
    openProgress();
    $.ajax({
        url: "/portal/admin/rolefunction/queryGnListByJsId.do",
        data: qqcs,
        type: "post",
        timeout: sys_timeout,
        dataType: "json",
        async: false,
        error: function (XMLHttpRequest, textStatus) {
            closeProgress();
            innerToast.warn("抱歉，系统暂时无法访问,请稍后再试");
            console.log(textStatus);
        },
        success: function (respText) {
            closeProgress();
            if (checkResponse(respText)) {
                //更新功能树
                $('#gn_tree').tree({
                    onlyLeafCheck: false,//仅叶子节点可以被选中
                    checkbox: true,//复选框
                    data: respText.repData.treelist,
                    onLoadSuccess: function () {
                        $("#gn_tree").tree("collapseAll");
                    },
                    onCheck: function (node, checked) {
                        var childList = $(this).tree('getChildren', node.target);
                        if (childList.length > 0) {
                            var checkedTrue = function () {
                                childList.map(function (currentValue) {
                                    $("div[node-id='" + currentValue.id + "']").find(".tree-checkbox").removeClass("tree-checkbox0").addClass("tree-checkbox1");
                                })
                            };
                            var checkedFalse = function () {
                                $.each(childList, function (index, currentValue) {
                                    $("div[node-id='" + currentValue.id + "']").find(".tree-checkbox").removeClass("tree-checkbox1").addClass("tree-checkbox0");
                                })
                            };
                            var checkChangeProperties = checked == true ? checkedTrue() : checkedFalse();
                        }
                    }
                });
                tmp_jsid = jsid;
            }
            else {
                innerToast.warn(respText.msg);
            }
        }
    });
}

//保存数据
function saveData() {
    if ("" == tmp_jsid) {
        innerToast.warn("请先选择角色!");
        return;
    }
    var gnNodes = $('#gn_tree').tree('getChecked');
    var str_gnlist = "";
    for (var i = 0; i < gnNodes.length; i++) {
        //菜单
        if (gnNodes[i].open_type != "0") {
            if ("" == str_gnlist) {
                str_gnlist = gnNodes[i].id;
            } else {
                str_gnlist = str_gnlist + "," + gnNodes[i].id;
            }
        }
    }
    qqcs = {"jsid": tmp_jsid, "str_gnlist": str_gnlist};
    openProgress();
    $.ajax({
        url: "/portal/admin/rolefunction/saveData.do",
        data: qqcs,
        type: "post",
        timeout: sys_timeout,
        dataType: "json",
        async: false,
        error: function (XMLHttpRequest, textStatus) {
            closeProgress();
            innerToast.warn("抱歉，系统暂时无法访问,请稍后再试");
            console.log(textStatus);
        },
        success: function (respText) {
            closeProgress();
            if (checkResponse(respText)) {
                innerToast.info("保存成功!");
            }
            else {
                innerToast.warn(respText.msg);
            }
        }
    });
}

function openProgress() {
    $.messager.progress({
        title: '提示',
        msg: '请稍候……',
        text: ''
    });
}

function closeProgress() {
    $.messager.progress('close');
}